package com.arvato.minipro.controller.admin.mall;

import java.util.Date;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.arvato.minipro.bean.bean_do.mall.EcCommodityDo;
import com.arvato.minipro.bean.bean_dto.common.PageData;
import com.arvato.minipro.bean.bean_dto.common.Query;
import com.arvato.minipro.bean.bean_dto.common.Result;
import com.arvato.minipro.bean.bean_vo.mall.EcCommodityVo;
import com.arvato.common.ftp.FtpTool;
import com.arvato.minipro.service.mall.CommodityService;

/**
 * Describe：后台管理商品的Controller
 *
 * @author smallking
 *
 *  2018年2月8日
 */
@Controller
@RequestMapping(value = "/admin/mall/commodity")
public class AdminCommodityController {
	
	@Autowired
	private CommodityService commodityService;
	
	@Autowired
	private FtpTool ftptool;
	
	/**
	 * 查询商品列表
	 * @param param
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/list", method = {RequestMethod.POST})
	public PageData list(@RequestParam Map<String, Object> param){
		Query query = new Query(param);
		PageData pageData = commodityService.listCommodityByQuery(query, query.getPageNum(), query.getPageSize());
		return pageData;
	}
	
	/**
	 * 查询商品列表
	 * @param param
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/detail", method = {RequestMethod.POST})
	public Result detail(String commodityId){
		Result result = new Result(true);
		EcCommodityVo commodityVo = commodityService.getCommodityById(commodityId);
		result.setData(commodityVo);
		return result;
	}
	
	/**
	 * 新增一个商品
	 * @param commodityDo
	 * @param imgIdList 商品图片ID数组
	 * @param categoryId 商品类别ID
	 * @param effectIdList 商品功能列表
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/insert", method = {RequestMethod.POST})
	public Result insert(@RequestParam("introduce_file") MultipartFile commodityImg, @RequestParam("commodityImgBigFile") MultipartFile commodityBigImg, EcCommodityDo commodityDo,
		String categoryId, String[] imgIdList, String[] effectIdList){
		Result result = new Result(false);
		// 存储在远程服务器上
		// 商品详情图
		String fileName = ftptool.uploadFile(commodityImg);
		if(fileName != null){
			commodityDo.setDetailImgPath(fileName);
		}
		// 商品大图
		String commodityBigImgName = ftptool.uploadFile(commodityBigImg);
		if(commodityBigImgName != null){
			commodityDo.setCommodityBigImg(commodityBigImgName);
		}
		try {
			// TODO 目前后台没登录，后期增加只有商户管理员才能登录后台 commodityDo.setUserId的值为当登录后台系统的人的ID
			commodityDo.setUserId("001");
			commodityDo.setCreateTime(new Date());
			int row = commodityService.insert(commodityDo, imgIdList, categoryId, effectIdList);
			if(row == 1){
				result.setResult(true);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 修改一个商品
	 * @param file
	 * @param commodityDo
	 * @param categoryId
	 * @param effectIdList
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/update", method = {RequestMethod.POST})
	public Result update(@RequestParam("introduce_file") MultipartFile file, @RequestParam("commodityImgBigFile") MultipartFile commodityBigImg, EcCommodityDo commodityDo, String categoryId, String[] effectIdList){
		Result result = new Result(false);
		// 存储在远程服务器上
		String fileName = ftptool.uploadFile(file);
		if(fileName != null){
			commodityDo.setDetailImgPath(fileName);
		}
		String commodityBigImgName = ftptool.uploadFile(commodityBigImg);
		if(commodityBigImgName != null){
			commodityDo.setCommodityBigImg(commodityBigImgName);
		}
		try {
			commodityDo.setUpdateTime(new Date());
			int row = commodityService.update(commodityDo, categoryId, effectIdList);
			if(row == 1){
				result.setResult(true);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 删除一个商品
	 * @param commodityId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/delete", method = {RequestMethod.POST})
	public Result delete(String commodityId){
		Result result = new Result(false);
		try {
			int row = commodityService.delete(commodityId);
			if(row == 1){
				result.setResult(true);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
}
